package com.wanban.adutils.utils

import android.app.Activity
import com.wanban.adutils.callback.OnLoadAdCallback


/**
 * 开屏广告
 * 包括 Splash广告和插屏广告
 * 使用 AdConstants.ADTYPE_OPEN：表示使用普通的插屏广告
 * 使用 AdConstants.AD_ID_SPLASH：表示单独使用Admob的Splash广告
 */
object OpenAdTools {

    private val openAdTools by lazy { CommonAdTools() }
    fun loadAd(activity: Activity, adId: Long, callback: OnLoadAdCallback?){
        if (isAdLoadSuccess()) {
            callback?.onAdLoaded()
            return
        }
        openAdTools.loadAd(activity, adId, object: OnLoadAdCallback(){
            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                callback?.onAdLoaded()
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                callback?.onAdLoadFailed(errorCode)
                release()
            }

            override fun onAdClicked() {
                super.onAdClicked()
                callback?.onAdClicked()
            }

            override fun onAdOpened() {
                super.onAdOpened()
                callback?.onAdOpened()
            }

            override fun onAdComplete() {
                super.onAdComplete()
                callback?.onAdComplete()
                release()
            }
        })
    }

    fun isAdLoadSuccess(): Boolean{
        return openAdTools.isAdLoadSuccess()
    }


    fun showAd(activity: Activity){
        if (activity.isFinishing || activity.isDestroyed) return
        if (!openAdTools.isAdLoadSuccess()) {
            return
        }
        openAdTools.showAdInActivity(activity)
    }


    fun release(){
        openAdTools.release()
    }

}